package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.model.data.LanguageTrailInfo
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import kotlinx.android.synthetic.main.activity_language_trail.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException

/**
 *@author Admin
 *@date 2021/1/7
 *
 */
class LanguageTrailActivity : BaseActivity() {
    private val tabs = arrayOf("综合", "听力", "口语", "阅读", "写作")
    private val stuID by lazy { intent.getStringExtra("stuID") }
    override fun setLayout(): Int {
        return R.layout.activity_language_trail
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        getLanguageHisChange()
    }

    override fun isNeedToolBar(): Boolean {
        return false
    }

    private fun initFragment(trailInfo: LanguageTrailInfo) {
        vp.isUserInputEnabled = false
        vp.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return 5
            }

            override fun createFragment(position: Int): Fragment {
                return LanguageTrailFragment().apply {
                    arguments = when (position) {
                        0 -> bundleOf(Pair("trail", trailInfo.ComprehensiveList))
                        1 -> bundleOf(Pair("trail", trailInfo.HearingList))
                        2 -> bundleOf(Pair("trail", trailInfo.LanguageList))
                        3 -> bundleOf(Pair("trail", trailInfo.ReadingList))
                        else -> bundleOf(Pair("trail", trailInfo.WritingList))
                    }
                }
            }
        }
        TabLayoutMediator(tbl, vp) { tab, position ->
            tab.text = tabs[position]
        }.attach()
        val position = when {
            trailInfo.ComprehensiveList.CognitiveAbility.isNotEmpty() -> 0
            trailInfo.HearingList.CognitiveAbility.isNotEmpty() -> 1
            trailInfo.LanguageList.CognitiveAbility.isNotEmpty() -> 2
            trailInfo.ReadingList.CognitiveAbility.isNotEmpty() -> 3
            else -> 4
        }
        tbl.getTabAt(position)?.select()
    }

    @SuppressLint("CheckResult")
    fun getLanguageHisChange() {
        val method = BkApiMethods(this)
        val params = BkApiMethods.Paramas()
        params["stuID"] = stuID
        method.getLanguageHisChange(Ext.RZZD_URL, params).subscribe({
            if (it.Code == 1) {
                initFragment(it.Data)
            } else {
                MyToast.showToast( it.Message)
            }
        }, {
            val error: String = if (it is TimeoutException || it is SocketTimeoutException || it is ConnectException) {
                getString(R.string.aibk_network_timeout)
            } else {
                getString(R.string.aibk_server_sick)
            }
            MyToast.showToast( error)
        })
    }
}